package com.soft.link.impl;

import com.soft.link.mapper.TsRoleMenuMapper;
import com.soft.link.model.*;
import com.soft.link.service.TsRoleMenuService;
import com.soft.link.utils.UniqueUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * company 重庆庆云石油工程技术有限责任公司
 * FileName TsRoleMenuServiceImpl
 * Package com.soft.link.impl
 * Description 角色权限接口实现类
 * author Administrator
 * create 2018-03-31 22:40
 * version V1.0
 */
@Service
public class TsRoleMenuServiceImpl implements TsRoleMenuService {
    //public Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    TsRoleMenuMapper tsRoleMenuMapper;

    /**
     * @param tsUserRoleList
     * @methodname findTsRoleMenuList
     * @Description 根据人员角色关联查询 人员角色菜单关系
     * @author 黄学乾
     * @create 2018/3/31/031 22:41
     */
    @Override
    public List<TsRoleMenu> findTsRoleMenuList(List<TsUserRole> tsUserRoleList) throws Exception {
        List<TsRoleMenu> tsRoleMenuList = tsRoleMenuMapper.findTsRoleMenuList(tsUserRoleList);
        return tsRoleMenuList;
    }

    /**
     * @throws   {如果有异常说明请填写}
     * @methodname   findTsRoleMenuListByTsRole
     * @Description {根据角色查询当前角色所有的关联菜单}
     * @author 黄学乾
     * @create 2018/4/27/027 11:23
     */
    @Override
    public List<TsRoleMenu> findTsRoleMenuListByTsRole(TsRole tsRole) throws Exception {
        return tsRoleMenuMapper.findTsRoleMenuByTsRole(tsRole);
    }

    /**
     * @param roleId       角色id
     * @param treeViewList
     * @return
     * @throws {如果有异常说明请填写}
     * @methodname insertTsRoleMenu
     * @Description {填写方法注释说明}
     * @author 黄学乾
     * @create 2018/4/28/028 16:36
     */
    @Override
    @Transactional
    public int insertTsRoleMenu(String roleId, List<TreeView> treeViewList, TsUser tsUser) throws Exception {
        int resInt = 0;
        if (StringUtils.isNotEmpty(roleId) && treeViewList != null && treeViewList.size() > 0) {
            for(TreeView tv : treeViewList){
                 TsRoleMenu ts = new TsRoleMenu();
                 ts.setMenuId(tv.getId());
                 ts.setRoleId(roleId);
                 ts.setRoleMenuId(UniqueUtil.uuid());
                 ts.setStatus(0);
                 ts.setCreatorId(tsUser.getUserId());
                 ts.setCreator(tsUser.getUserName());
                 ts.setModifyerId(tsUser.getUserId());
                 ts.setModifyer(tsUser.getUserName());
                 ts.setCreateTime(new Date());
                 ts.setModifyTime(new Date());
                 resInt = resInt + tsRoleMenuMapper.insertSelective(ts);
            }
        }
        return resInt;
    }
}
